# coding:utf-8
# 创建一个复杂的GUI
# 加入time.sleep()控制进度栏
import time

from PyQt5 import QtCore, QtWidgets, QtGui
from QtUI05 import complex
import sys


class MainWindow(object):
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        self.ui = complex.Ui_MainWindow()
        self.ui.setupUi(MainWindow)

        self.update_date()
        self.update_calendar()

        self.set_dial()
        self.set_lcd()

        # self.zero_process()

        # self.click_radio3()

        self.update_progressbar()

        self.set_font()

        MainWindow.show()
        sys.exit(app.exec_())

    # 修改日期修改器数值
    def update_date(self):
        self.ui.dateEdit.setDate(self.ui.calendarWidget.selectedDate())

    # 日历信号槽
    def update_calendar(self):
        self.ui.calendarWidget.selectionChanged.connect(self.update_date)

    # 设置LCD数字
    def set_lcd(self):
        self.ui.lcdNumber.display(self.ui.dial.value())
        # print(self.ui.dial.value())

    # 刻度盘信号槽
    def set_dial(self):
        self.ui.dial.valueChanged['int'].connect(self.set_lcd)

    # # 设置进度栏为零
    # def zero_process(self):
    #     self.ui.radioButton_2.clicked.connect(self.ui.progressBar.reset)
    #
    # # 更新进度栏
    # def update_process(self):
    #     value = self.ui.lcdNumber.value()
    #     self.ui.progressBar.setValue(value)
    #
    # # 点击按钮3
    # def click_radio3(self):
    #     self.ui.radioButton_3.clicked.connect(self.update_process)

    # 按钮信息槽
    def update_progressbar(self):
        self.ui.radioButton.clicked.connect(self.start_progressbar)
        self.ui.radioButton_2.clicked.connect(self.stop_progressbar)
        self.ui.radioButton_3.clicked.connect(self.rest_progressbar)

    # 启动进度栏
    def start_progressbar(self):
        self.stop_progress = False
        self.progress_value = self.ui.progressBar.value()

        # 通过while循环来增加进度栏，每次增加0.0001
        # while(self.progress_value <= 100.1) and not (self.stop_progress):
        #     self.ui.progressBar.setValue(self.progress_value)
        #     self.progress_value += 0.0001
        #     QtWidgets.QApplication.processEvents()

        # 通过time模块的sleep()方法冻结进度栏，以达到降低进度栏增长速度的效果
        while (self.progress_value <= 101) and not (self.stop_progress):
            self.ui.progressBar.setValue(self.progress_value)
            self.progress_value += 1
            time.sleep(0.3)
            QtWidgets.QApplication.processEvents()

    # 停止进度栏
    def stop_progressbar(self):
        self.stop_progress = True

    # 重设进度栏
    def rest_progressbar(self):
        self.progress_value = 0
        self.ui.progressBar.reset()
        self.stop_progress = False

    # 字体选择
    def set_font(self):
        self.ui.fontComboBox.activated['QString'].connect(self.ui.label.setText)


if __name__ == "__main__":
    MainWindow()
